﻿using Microsoft.AspNetCore.Http;
using RsCms.Infrastructure;
using RsCode;

namespace RsCms.Core.Pages
{
    public class PageDomainService:IPageDomainService
    {
        IPostsRepository repository;
        IHttpContextAccessor httpContextAccessor;
        public PageDomainService(IPostsRepository repository,
            IHttpContextAccessor httpContextAccessor)
        {
            this.repository = repository;
            this.httpContextAccessor = httpContextAccessor;
        }

        string LoginUserId
        {
            get
            {
                var claims = httpContextAccessor?.HttpContext?.User.Claims;
                return claims?.First(x => x.Type == "UserId").Value ?? "";
            }
        }
       
        /// <summary>
        /// 查询指定id或slug相同的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entities.Posts> GetPageInfoAsync(string id)
        {
            return await repository.PostStore.Where(x => x.Id == id||x.Slug==id).ToOneAsync();
        }

        

        public async Task<Entities.Posts> SaveAsync(Entities.Posts posts)
        {
            if (posts == null)
            {
                throw new AppException("提交参数不能为空");
            }

             
            var info = await repository.PostStore.Where(x => x.Id == posts.Id).ToOneAsync();
            if (info == null)
            {
                posts.Init(LoginUserId); 
                posts.Type = "page";
                if(string.IsNullOrEmpty(posts.Slug))
                {
                   posts.Slug= StrHelper.CreateSlug(posts.Title);
                }
                return repository.PostStore.Insert(posts);
            }
            else
            {
                posts.Type = "page";
                posts.CanonicalUrl = posts.CanonicalUrl != info.CanonicalUrl ? posts.CanonicalUrl : info.CanonicalUrl;
                posts.CodeinjectionFoot = posts.CodeinjectionFoot != info.CodeinjectionFoot ? posts.CodeinjectionFoot : info.CodeinjectionFoot;
                posts.CodeinjectionHead = posts.CodeinjectionHead != info.CodeinjectionHead ? posts.CodeinjectionHead : info.CodeinjectionHead;
                posts.CommentId = posts.CommentId != info.CommentId ? posts.CommentId : info.CommentId;
                posts.CustomExcerpt = posts.CustomExcerpt != info.CustomExcerpt ? posts.CustomExcerpt : info.CustomExcerpt;
                posts.CustomTemplate = posts.CustomTemplate != info.CustomTemplate ? posts.CustomTemplate : info.CustomTemplate;
                posts.EmailRecipientFilter = posts.EmailRecipientFilter != info.EmailRecipientFilter ? posts.EmailRecipientFilter : info.EmailRecipientFilter;
                posts.Featured = posts.Featured != info.Featured ? posts.Featured : info.Featured;
                posts.FeatureImage = posts.FeatureImage != info.FeatureImage ? posts.FeatureImage : info.FeatureImage;
                posts.Html = posts.Html != info.Html ? posts.Html : info.Html;
                posts.Markdown = posts.Markdown != info.Markdown ? posts.Markdown : info.Markdown;
                posts.Lexical = posts.Lexical != info.Lexical ? posts.Lexical : info.Lexical;
                posts.Locale = posts.Locale != info.Locale ? posts.Locale : info.Locale;
                posts.Mobiledoc = posts.Mobiledoc != info.Mobiledoc ? posts.Mobiledoc : info.Mobiledoc;
                posts.Plaintext = posts.Plaintext != info.Plaintext ? posts.Plaintext : info.Plaintext;
                posts.ShowTitleAndFeatureImage = posts.ShowTitleAndFeatureImage != info.ShowTitleAndFeatureImage ? posts.ShowTitleAndFeatureImage : info.ShowTitleAndFeatureImage;
                posts.Slug = posts.Slug != info.Slug ? posts.Slug : info.Slug;
                posts.Title = posts.Title != info.Title ? posts.Title : info.Title;
                posts.Type = posts.Type != info.Type ? posts.Type : info.Type;
                posts.UpdatedBy = posts.UpdatedBy != info.UpdatedBy ? posts.UpdatedBy : info.UpdatedBy;
                posts.Visibility = posts.Visibility != info.Visibility ? posts.Visibility : info.Visibility;
                posts.Tags = posts.Tags != info.Tags ? posts.Tags : info.Tags;
                posts.Status = posts.Status != info.Status ? posts.Status : info.Status;

                posts.Uuid = LoginUserId;
                posts.CreatedAt = info.CreatedAt;
                posts.CreatedBy = info.CreatedBy;
                posts.PublishedBy = info.PublishedBy;
                posts.PublishedAt = info.PublishedAt;
                posts.UpdatedAt = DateTime.Now;
                if (string.IsNullOrEmpty(posts.Slug))
                {
                    posts.Slug = StrHelper.CreateSlug(posts.Title);
                }

                repository.PostStore.Update(posts);
                return posts;
            }

        }

        

        public async Task<PageData<Entities.Posts>> PageAsync(int page, int pageSize)
        {
            if (pageSize > 30) pageSize = 30;

            var select = repository.PostStore.Select.Where(x => x.Type == "page").OrderByDescending(x => x.CreatedAt).OrderBy(x=>x.Status);
            return await repository.PostStore.PageAsync(page, pageSize, select);
        }

      
    }
}
